#!/bin/bash

# Tiny QA - 带文档目录挂载的Docker启动脚本

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 配置
IMAGE_NAME="tiny-qa:latest"
CONTAINER_NAME="tiny-qa"
PORT="5002"

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}Tiny QA - 带文档目录挂载启动${NC}"
echo -e "${BLUE}========================================${NC}"

# 检查Docker是否运行
if ! docker info > /dev/null 2>&1; then
    echo -e "${RED}❌ Docker未运行，请先启动Docker Desktop${NC}"
    exit 1
fi

# 检查镜像是否存在
if ! docker images | grep -q "tiny-qa.*latest"; then
    echo -e "${RED}❌ 镜像 ${IMAGE_NAME} 不存在${NC}"
    echo -e "${YELLOW}💡 请先构建镜像:${NC}"
    echo -e "  ./scripts/docker-build-stable.sh"
    exit 1
fi

# 停止并删除现有容器
if docker ps -a | grep -q "$CONTAINER_NAME"; then
    echo -e "${YELLOW}🛑 停止现有容器...${NC}"
    docker stop "$CONTAINER_NAME" 2>/dev/null || true
    docker rm "$CONTAINER_NAME" 2>/dev/null || true
fi

# 获取文档目录路径
echo -e "${YELLOW}📁 配置文档目录挂载${NC}"
echo -e "${BLUE}请输入您的文档目录路径:${NC}"
echo -e "  示例:"
echo -e "    macOS: /Users/username/Documents"
echo -e "    Windows: C:\\Users\\username\\Documents"
echo -e "    Linux: /home/username/documents"
echo ""

read -p "文档目录路径: " DOCS_PATH

# 验证路径
if [[ -z "$DOCS_PATH" ]]; then
    echo -e "${RED}❌ 文档目录路径不能为空${NC}"
    exit 1
fi

# 检查路径是否存在（在主机上）
if [[ ! -d "$DOCS_PATH" ]]; then
    echo -e "${RED}❌ 文档目录不存在: $DOCS_PATH${NC}"
    echo -e "${YELLOW}💡 请检查路径是否正确${NC}"
    exit 1
fi

echo -e "${GREEN}✅ 文档目录验证通过: $DOCS_PATH${NC}"

# 创建必要的数据目录
echo -e "${YELLOW}📁 创建数据目录...${NC}"
mkdir -p data/database data/vectors logs

# 启动容器
echo -e "${YELLOW}🚀 启动容器...${NC}"
echo -e "${BLUE}挂载配置:${NC}"
echo -e "  主机路径: $DOCS_PATH"
echo -e "  容器路径: /app/data/documents"
echo ""

docker run -d \
    --name "$CONTAINER_NAME" \
    -p "$PORT:5002" \
    -v "$(pwd)/data:/app/data" \
    -v "$(pwd)/logs:/app/logs" \
    -v "$(pwd)/config:/app/config" \
    -v "$DOCS_PATH:/app/data/documents" \
    "$IMAGE_NAME"

# 检查容器状态
sleep 3
if docker ps | grep -q "$CONTAINER_NAME"; then
    echo -e "${GREEN}✅ 容器启动成功！${NC}"
    echo ""
    echo -e "${BLUE}📋 容器信息:${NC}"
    docker ps | grep "$CONTAINER_NAME"
    echo ""
    echo -e "${BLUE}🌐 访问地址:${NC}"
    echo -e "  主页: http://localhost:$PORT"
    echo -e "  首次配置: http://localhost:$PORT/first-run"
    echo ""
    echo -e "${BLUE}📁 文档目录:${NC}"
    echo -e "  主机路径: $DOCS_PATH"
    echo -e "  容器路径: /app/data/documents"
    echo ""
    echo -e "${BLUE}🔍 查看日志:${NC}"
    echo -e "  docker logs -f $CONTAINER_NAME"
    echo ""
    echo -e "${BLUE}🛑 停止容器:${NC}"
    echo -e "  docker stop $CONTAINER_NAME"
else
    echo -e "${RED}❌ 容器启动失败${NC}"
    echo -e "${YELLOW}💡 查看错误日志:${NC}"
    echo -e "  docker logs $CONTAINER_NAME"
    exit 1
fi

# 验证文档目录挂载
echo -e "${YELLOW}🔍 验证文档目录挂载...${NC}"
sleep 2
if docker exec "$CONTAINER_NAME" ls -la /app/data/documents | grep -q -v "^total"; then
    echo -e "${GREEN}✅ 文档目录挂载成功${NC}"
    echo -e "${BLUE}📄 文档文件:${NC}"
    docker exec "$CONTAINER_NAME" find /app/data/documents -type f \( -name "*.pdf" -o -name "*.docx" -o -name "*.xlsx" -o -name "*.txt" -o -name "*.md" \) | head -5
else
    echo -e "${YELLOW}⚠️  文档目录为空或挂载失败${NC}"
    echo -e "${BLUE}💡 请检查:${NC}"
    echo -e "  1. 文档目录路径是否正确"
    echo -e "  2. 目录是否包含支持格式的文档"
    echo -e "  3. Docker权限是否足够"
fi

echo ""
echo -e "${GREEN}🎉 启动完成！${NC}" 